package com.example.system.service.question.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.common.core.constants.Constants;
import com.example.common.core.domain.R;
import com.example.common.core.enums.ResultCode;
import com.example.common.security.exception.ServiceException;
import com.example.system.domain.question.Question;
import com.example.system.domain.question.dto.QuestionAddDTO;
import com.example.system.domain.question.dto.QuestionEditDTO;
import com.example.system.domain.question.dto.QuestionQueryDTO;
import com.example.system.domain.question.es.QuestionES;
import com.example.system.domain.question.vo.QuestionDetailVO;
import com.example.system.domain.question.vo.QuestionVO;
import com.example.system.elasticsearch.QuestionRepository;
import com.example.system.manager.QuestionCacheManager;
import com.example.system.mapper.question.QuestionMapper;
import com.example.system.service.question.QuestionService;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Slf4j
@Service
public class QuestionServiceImpl implements QuestionService {
    @Autowired
    private QuestionMapper questionMapper;

    @Autowired
    private QuestionRepository questionRepository;

    @Autowired
    private QuestionCacheManager questionCacheManager;
    @Override
    public List<QuestionVO> list(QuestionQueryDTO questionQueryDTO) {
        if (StrUtil.isNotEmpty(questionQueryDTO.getExcludeIdStr())) {
            String[] excludeIds = questionQueryDTO.getExcludeIdStr().split(Constants.SPLIT_ID);
            Set<Long> excludeIdSet = Arrays.stream(excludeIds).map(Long::valueOf).collect(Collectors.toSet());
            questionQueryDTO.setExcludeIdSet(excludeIdSet);
        }
        PageHelper.startPage(questionQueryDTO.getPageNum(), questionQueryDTO.getPageSize());
        return questionMapper.selectQuestionList(questionQueryDTO);
    }

    @Override
    public boolean add(QuestionAddDTO questionAddDTO) {
        List<Question> questionList = questionMapper.selectList(new LambdaQueryWrapper<Question>().eq(Question::getTitle,questionAddDTO.getTitle()));
        if (CollectionUtil.isNotEmpty(questionList)) {
            throw new ServiceException(ResultCode.FAILED_ALREADY_EXISTS);
        }
        Question question = new Question();
        BeanUtil.copyProperties(questionAddDTO,question);

        int insert = questionMapper.insert(question);
        if (insert <= 0) {
            return false;
        }
        updataES(question);
        questionCacheManager.addCache(question.getId());
        return true;
    }



    @Override
    public boolean delete(Long id) {
        Question question = questionMapper.selectById(id);
        if (question == null) {
            throw  new ServiceException(ResultCode.FAILED_NOT_EXISTS);
        }
        int i = questionMapper.deleteById(id);
        if (i <= 0) {
            return false;
        }
        deleteES(question);
        questionCacheManager.removeCache(id);
        return true;
    }

    private void deleteES(Question question) {
        questionRepository.deleteById(question.getId());
    }

    @Override
    public R<QuestionDetailVO> detail(Long id) {
        Question question = questionMapper.selectById(id);
        if (question == null) {
            throw new ServiceException(ResultCode.FAILED_NOT_EXISTS);
        }
        QuestionDetailVO questionDetailVO = new QuestionDetailVO();
        BeanUtil.copyProperties(question,questionDetailVO);
        return R.ok(questionDetailVO);
    }

    @Override
    public boolean update(QuestionEditDTO questionEditDTO) {
        Question question = questionMapper.selectById(questionEditDTO.getId());
        if (question == null) {
            throw new ServiceException(ResultCode.FAILED_NOT_EXISTS);
        }
        BeanUtil.copyProperties(questionEditDTO,question);
        int i = questionMapper.updateById(question);
        if (i <= 0) {
            return false;
        }
        updataES(question);
        return true;

    }
    private void updataES(Question question) {
        QuestionES questionES = new QuestionES();
        BeanUtil.copyProperties(question,questionES);
        if (question.getMainFunction() != null) {
            questionES.setMainFunc(question.getMainFunction());
        }
        questionRepository.save(questionES);
    }
}
